/*
 * SettingsPanel.java
 *
 * Created on March 2, 2005, 10:52 AM
 */
package com.bff.mpd.musicjukebox.ui.settings;

import com.bff.bjj.lib.actions.BugTrackerAction;
import com.bff.bjj.lib.actions.DiscussAction;
import com.bff.bjj.lib.actions.HomeAction;
import com.bff.bjj.lib.actions.WikiAction;
import com.bff.bjj.ui.GroupByFolderCheckBox;
import com.bff.bjj.ui.LAFPanel;
import com.bff.bjj.ui.component.BJJButton;
import com.bff.bjj.ui.component.BJJPanel;
import com.bff.bjj.utils.BrowserLauncher;
import com.bff.mpd.musicjukebox.lib.JukeboxProperties;
import com.bff.mpd.musicjukebox.lib.StartMPD;
import com.bff.mpd.musicjukebox.lib.actions.ManageOutputsAction;
import com.bff.mpd.musicjukebox.ui.AboutDialog;
import com.bff.mpd.musicjukebox.ui.SplashScreen;
import com.bff.mpd.musicjukebox.ui.database.MPDUpdateDialog;
import com.bff.mpd.utils.Utils;
import com.bff.startup.StartApp;
import com.bff.startup.StartUpProperties;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.Action;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import org.bff.javampd.MPD;

/**
 *
 * @author  bill
 */
public class SettingsPanel extends BJJPanel {

    private JukeboxProperties jp;
    private static final String EXIT_STOP = "Stop Player";
    private static final String EXIT_NOTHING = "Do Nothing";
    private boolean firstTime = false;
    private boolean connectionSuccessful = false;
    private JPanel panel;
    private SplashScreen sp;
    private DefaultComboBoxModel exitModel;
    private boolean restartRequired;
    private Action outputsAction;

    public SettingsPanel() {
        this(false);
    }

    /** Creates new form SettingsPanel */
    public SettingsPanel(boolean firstTime) {
        super();

        exitModel = new DefaultComboBoxModel();
        exitModel.addElement(EXIT_STOP);
        exitModel.addElement(EXIT_NOTHING);

        jp = JukeboxProperties.getInstance();

        makeActions();

        initComponents();

        this.firstTime = firstTime;
        if (!firstTime) {
            //fill in the fields
            setMpdServer(jp.getMpdServer());
            setMpdPort(Integer.toString(jp.getMpdPort()));
            setPassword(jp.getMPDPassword());
            setShowGuitarPanel(jp.isShowGuitarPanel());
            setMinimizeToTray(jp.isTrayMin());
            setCheckForUpdate(jp.isCheckForUpdate());
            setExitAction(jp.isStopOnExit() ? EXIT_STOP : EXIT_NOTHING);
            setUseFileNameForUnknown(jp.isUseFileNameForUnknown());
            setRememberState(StartUpProperties.getInstance().isUseLastState());
            setIncludeThe(jp.isIncludeThe());
            setTrayAlways(jp.isTrayAlways());
            setGroupByFolder(jp.isGroupByFolder());
            if (!jp.isTrayAlways() && !jp.isTrayMin()) {
                setTrayNever(true);
            }
        } else {
            setTrayNever(true);
            remove(btnOutput);
        }

        btnRefresh.setVisible(!firstTime);

        if (jp.isCheckForUpdate()) {
            new Thread(new Runnable() {

                @Override
                public void run() {
                    try {
                        if (jp.isDeveloper()) {
                            lblUpdate.setVisible(true);
                            lblUpdate.setText("In Developer Mode");
                            btnGetUpdate.setVisible(true);
                        } else if (jp.isSnapShot()) {
                            lblUpdate.setVisible(true);
                            lblUpdate.setText("Using SnapShot build!");
                            btnGetUpdate.setVisible(true);
                        } else if (!jp.getVersion().equalsIgnoreCase(jp.versionAvailable())) {
                            lblUpdate.setVisible(true);
                            lblUpdate.setText("An update is available");
                            btnGetUpdate.setVisible(true);
                        }
                    } catch (IOException ex) {
                        Logger.getLogger(SettingsPanel.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }).start();
        }
        panel = this;

        setColors();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        trayButtonGroup = new javax.swing.ButtonGroup();
        settingsPanel = new BJJPanel();
        jPanel3 = new BJJPanel();
        jLabel7 = new javax.swing.JLabel();
        mpdServerTextField = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        mpdPortTextField = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        cbPassword = new javax.swing.JCheckBox();
        tfPassword = new javax.swing.JPasswordField();
        jLabel16 = new javax.swing.JLabel();
        cbExitAction = new javax.swing.JComboBox();
        lblUpdate = new javax.swing.JLabel();
        lblUpdate.setVisible(false);
        btnGetUpdate = new BJJButton();
        btnGetUpdate.setVisible(false);
        lafPanel = new LAFPanel();
        jPanel4 = new BJJPanel();
        chkboxUseFileNameForUnknown = new javax.swing.JCheckBox();
        chkboxCheckForUpdate = new javax.swing.JCheckBox();
        chkboxGuitarPanel = new javax.swing.JCheckBox();
        chkboxLastState = new javax.swing.JCheckBox();
        chkboxIncludeThe = new javax.swing.JCheckBox();
        jPanel5 = new BJJPanel();
        chkboxMinTray = new javax.swing.JCheckBox();
        chkboxTrayAlways = new javax.swing.JCheckBox();
        chkboxTrayNever = new javax.swing.JCheckBox();
        chkboxGroupByFolder = new GroupByFolderCheckBox();
        btnOutput = new BJJButton(outputsAction);
        jPanel1 = new BJJPanel();
        jPanel2 = new BJJPanel();
        btnBugReport = new BJJButton();
        btnHomePage = new BJJButton();
        btnWiki = new BJJButton();
        btnDiscuss = new BJJButton();
        buttonPanel = new BJJPanel();
        saveButton = new BJJButton();
        btnRefresh = new BJJButton();
        btnAbout = new BJJButton();

        setLayout(new java.awt.BorderLayout());

        settingsPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("MPD Settings"));
        settingsPanel.setLayout(new java.awt.BorderLayout());

        jPanel3.setLayout(new java.awt.GridBagLayout());

        jLabel7.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
        jLabel7.setText("MPD Server:");
        jLabel7.setBorder(javax.swing.BorderFactory.createEmptyBorder(5, 5, 5, 5));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(2, 0, 2, 0);
        jPanel3.add(jLabel7, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(2, 0, 2, 0);
        jPanel3.add(mpdServerTextField, gridBagConstraints);

        jLabel6.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
        jLabel6.setText("MPD Port:");
        jLabel6.setBorder(javax.swing.BorderFactory.createEmptyBorder(5, 5, 5, 5));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(2, 0, 2, 0);
        jPanel3.add(jLabel6, gridBagConstraints);

        mpdPortTextField.setText("6600");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(2, 0, 2, 0);
        jPanel3.add(mpdPortTextField, gridBagConstraints);

        jLabel8.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
        jLabel8.setText("Password:");
        jLabel8.setBorder(javax.swing.BorderFactory.createEmptyBorder(5, 5, 5, 5));
        jLabel8.setEnabled(false);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(2, 0, 2, 0);
        jPanel3.add(jLabel8, gridBagConstraints);

        cbPassword.setText("Use Password");
        cbPassword.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        cbPassword.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbPasswordActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.insets = new java.awt.Insets(2, 0, 2, 0);
        jPanel3.add(cbPassword, gridBagConstraints);

        tfPassword.setEnabled(false);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(2, 0, 2, 0);
        jPanel3.add(tfPassword, gridBagConstraints);

        jLabel16.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
        jLabel16.setText("Exit Action:");
        jLabel16.setBorder(javax.swing.BorderFactory.createEmptyBorder(5, 5, 5, 5));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 6;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(5, 1, 5, 1);
        jPanel3.add(jLabel16, gridBagConstraints);

        cbExitAction.setModel(exitModel);
        cbExitAction.setPreferredSize(new java.awt.Dimension(150, 25));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 6;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 1, 5, 1);
        jPanel3.add(cbExitAction, gridBagConstraints);

        lblUpdate.setForeground(java.awt.Color.red);
        lblUpdate.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblUpdate.setText("An update is availble.");
        lblUpdate.setBorder(javax.swing.BorderFactory.createEmptyBorder(5, 5, 5, 5));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 8;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.insets = new java.awt.Insets(5, 0, 0, 0);
        jPanel3.add(lblUpdate, gridBagConstraints);

        btnGetUpdate.setText("Get Update");
        btnGetUpdate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnGetUpdateActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 9;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 5, 0);
        jPanel3.add(btnGetUpdate, gridBagConstraints);

        lafPanel.setOpaque(false);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        jPanel3.add(lafPanel, gridBagConstraints);

        jPanel4.setLayout(new java.awt.GridBagLayout());

        chkboxUseFileNameForUnknown.setSelected(true);
        chkboxUseFileNameForUnknown.setText("Use file name for Unknown Titles");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        jPanel4.add(chkboxUseFileNameForUnknown, gridBagConstraints);

        chkboxCheckForUpdate.setSelected(true);
        chkboxCheckForUpdate.setText("Check for update at start up");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        jPanel4.add(chkboxCheckForUpdate, gridBagConstraints);

        chkboxGuitarPanel.setText("Show Guitar Panel");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        jPanel4.add(chkboxGuitarPanel, gridBagConstraints);

        chkboxLastState.setText("Remember last state");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        jPanel4.add(chkboxLastState, gridBagConstraints);

        chkboxIncludeThe.setText("Use 'The' in sort");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        jPanel4.add(chkboxIncludeThe, gridBagConstraints);

        jPanel5.setBorder(javax.swing.BorderFactory.createTitledBorder("Tray Support"));
        jPanel5.setLayout(new java.awt.GridBagLayout());

        trayButtonGroup.add(chkboxMinTray);
        chkboxMinTray.setText("Minimize to tray");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        jPanel5.add(chkboxMinTray, gridBagConstraints);

        trayButtonGroup.add(chkboxTrayAlways);
        chkboxTrayAlways.setText("Always in tray");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        jPanel5.add(chkboxTrayAlways, gridBagConstraints);

        trayButtonGroup.add(chkboxTrayNever);
        chkboxTrayNever.setText("Never in Tray");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        jPanel5.add(chkboxTrayNever, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridheight = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.VERTICAL;
        jPanel4.add(jPanel5, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        jPanel4.add(chkboxGroupByFolder, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.gridwidth = 2;
        jPanel3.add(jPanel4, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 7;
        gridBagConstraints.gridwidth = 2;
        jPanel3.add(btnOutput, gridBagConstraints);

        settingsPanel.add(jPanel3, java.awt.BorderLayout.CENTER);

        jPanel1.setLayout(new java.awt.GridBagLayout());

        btnBugReport.setAction(new BugTrackerAction());
        jPanel2.add(btnBugReport);

        btnHomePage.setAction(new HomeAction());
        jPanel2.add(btnHomePage);

        btnWiki.setAction(new WikiAction());
        jPanel2.add(btnWiki);

        btnDiscuss.setAction(new DiscussAction());
        jPanel2.add(btnDiscuss);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 14;
        gridBagConstraints.gridwidth = 2;
        jPanel1.add(jPanel2, gridBagConstraints);

        buttonPanel.setLayout(new java.awt.GridBagLayout());

        saveButton.setText("Save Settings");
        saveButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveButtonActionPerformed(evt);
            }
        });
        buttonPanel.add(saveButton, new java.awt.GridBagConstraints());

        btnRefresh.setText("Refresh Database");
        btnRefresh.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRefreshActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        buttonPanel.add(btnRefresh, gridBagConstraints);

        btnAbout.setText("About");
        btnAbout.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAboutActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 0;
        buttonPanel.add(btnAbout, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 9;
        gridBagConstraints.gridwidth = 2;
        jPanel1.add(buttonPanel, gridBagConstraints);

        settingsPanel.add(jPanel1, java.awt.BorderLayout.SOUTH);

        add(settingsPanel, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents

    private void btnAboutActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAboutActionPerformed
        new AboutDialog(Utils.getParentComponent(this)).setVisible(true);
    }//GEN-LAST:event_btnAboutActionPerformed

    private void cbPasswordActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbPasswordActionPerformed
        if (cbPassword.isSelected()) {
            setPasswordFields(true);
        } else {
            setPasswordFields(false);
        }
    }//GEN-LAST:event_cbPasswordActionPerformed

    private void btnRefreshActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRefreshActionPerformed
        try {
            new MPDUpdateDialog(Utils.getParentComponent(this), jp.getAdmin(), true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }//GEN-LAST:event_btnRefreshActionPerformed

    private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveButtonActionPerformed
        String mpdServer = getMpdServer();
        String mpdPort = getMpdPort();
        String password = getPassword();

        String msg = "";

        if (mpdServerTextField.isVisible() && (mpdServer.length() < 1 || mpdPort.length() < 1)) {
            msg = "Must Enter MPD Information!\n";
        }

        if (cbPassword.isSelected() && password.length() < 1) {
            msg += "Must enter a password!";
        }

        if (!msg.equals("")) {
            JOptionPane.showMessageDialog(this, msg);
        } else {
            if (!saveSettings()) {
                JOptionPane.showMessageDialog(this, "Problem Writing File! Please Verify Information.");
            } else {
                settingsSaved();
            }
        }
    }//GEN-LAST:event_saveButtonActionPerformed

    private void btnGetUpdateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGetUpdateActionPerformed
        BrowserLauncher.openURL(jp.getUpdateURL());
}//GEN-LAST:event_btnGetUpdateActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAbout;
    private javax.swing.JButton btnBugReport;
    private javax.swing.JButton btnDiscuss;
    private javax.swing.JButton btnGetUpdate;
    private javax.swing.JButton btnHomePage;
    private javax.swing.JButton btnOutput;
    private javax.swing.JButton btnRefresh;
    private javax.swing.JButton btnWiki;
    private javax.swing.JPanel buttonPanel;
    private javax.swing.JComboBox cbExitAction;
    private javax.swing.JCheckBox cbPassword;
    private javax.swing.JCheckBox chkboxCheckForUpdate;
    private javax.swing.JCheckBox chkboxGroupByFolder;
    private javax.swing.JCheckBox chkboxGuitarPanel;
    private javax.swing.JCheckBox chkboxIncludeThe;
    private javax.swing.JCheckBox chkboxLastState;
    private javax.swing.JCheckBox chkboxMinTray;
    private javax.swing.JCheckBox chkboxTrayAlways;
    private javax.swing.JCheckBox chkboxTrayNever;
    private javax.swing.JCheckBox chkboxUseFileNameForUnknown;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel lafPanel;
    private javax.swing.JLabel lblUpdate;
    private javax.swing.JTextField mpdPortTextField;
    private javax.swing.JTextField mpdServerTextField;
    private javax.swing.JButton saveButton;
    private javax.swing.JPanel settingsPanel;
    private javax.swing.JPasswordField tfPassword;
    private javax.swing.ButtonGroup trayButtonGroup;
    // End of variables declaration//GEN-END:variables

    private void setMpdServer(String server) {
        mpdServerTextField.setText(server);
    }

    private String getMpdServer() {
        return (mpdServerTextField.getText().trim());
    }

    private void setMpdPort(String port) {
        mpdPortTextField.setText(port);
    }

    private String getMpdPort() {
        return (mpdPortTextField.getText().trim());
    }

    private boolean getShowGuitarPanel() {
        return (chkboxGuitarPanel.isSelected());
    }

    private void setShowGuitarPanel(boolean b) {
        chkboxGuitarPanel.setSelected(b);
    }

    private boolean getMinimizeToTray() {
        return (chkboxMinTray.isSelected());
    }

    private boolean getIncludeThe() {
        return (chkboxIncludeThe.isSelected());
    }

    private boolean getCheckForUpdate() {
        return (chkboxCheckForUpdate.isSelected());
    }

    private void setCheckForUpdate(boolean b) {
        chkboxCheckForUpdate.setSelected(b);
    }

    private void setMinimizeToTray(boolean b) {
        chkboxMinTray.setSelected(b);
    }

    private boolean getTrayAlways() {
        return chkboxTrayAlways.isSelected();
    }

    private void setTrayAlways(boolean trayAlways) {
        chkboxTrayAlways.setSelected(trayAlways);
    }

    private boolean getGroupByFolder() {
        return chkboxGroupByFolder.isSelected();
    }

    private void setGroupByFolder(boolean groupByFolder) {
        chkboxGroupByFolder.setSelected(groupByFolder);
    }

    private void setTrayNever(boolean trayNever) {
        chkboxTrayNever.setSelected(trayNever);
    }

    private void setUseFileNameForUnknown(boolean b) {
        chkboxUseFileNameForUnknown.setSelected(b);
    }

    private void setIncludeThe(boolean b) {
        chkboxIncludeThe.setSelected(b);
    }

    private boolean getUseFileNameForUnknown() {
        return chkboxUseFileNameForUnknown.isSelected();
    }

    private void hideLinkButtons() {
        jPanel2.setVisible(false);
    }

    private boolean saveSettings() {
        try {
            if (!jp.getMpdServer().equalsIgnoreCase(getMpdServer())
                    || jp.getMpdPort() != Integer.parseInt(getMpdPort().trim())) {
                setRestartRequired(true);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        jp.setMpdServer(getMpdServer());
        jp.setMpdPort(Integer.parseInt(getMpdPort()));
        if (cbPassword.isSelected()) {
            jp.setMPDPassword(getPassword());
        } else {
            jp.setMPDPassword(null);
        }

        jp.setShowGuitarPanel(getShowGuitarPanel());
        jp.setTrayMin(getMinimizeToTray());
        jp.setTrayAlways(getTrayAlways());
        jp.setCheckForUpdate(getCheckForUpdate());
        jp.setStopOnExit(getExitAction().equalsIgnoreCase(EXIT_STOP));
        jp.setUseFileNameForUnknown(getUseFileNameForUnknown());
        jp.setIncludeThe(getIncludeThe());
        jp.setGroupByFolder(getGroupByFolder());
        StartUpProperties.getInstance().saveSettings(getRememberState());

        return (jp.saveSettings());
    }

    private void settingsSaved() {
        if (!isFirstTime()) {
            if (isRestartRequired()) {
                int ch = JOptionPane.showConfirmDialog(this, "Settings change requires a restart.\nWould you like to restart?", "Restart", JOptionPane.YES_NO_OPTION);
                if (ch == JOptionPane.YES_OPTION) {
                    if (!StartApp.restartApp()) {
                        JOptionPane.showMessageDialog(this, "Could not restart the application.", "Error", JOptionPane.ERROR_MESSAGE);
                    }
                }
            } else {
                JOptionPane.showMessageDialog(this, "Settings Saved Successfully.\n"
                        + "Please restart Bill's Jammin' Jukebox for the settings to take effect!");
            }
        } else {
            sp = new SplashScreen(false);
            sp.setIndeterminate(true);
            sp.setTitleLabel("Connecting");
            sp.setMainTextArea("Testing connection to MPD\n\nOne moment.");
            sp.setProgressBarLabel("Connecting...");
            sp.pack();

            Thread t = new Thread(new MPDTester());
            t.start();
        }
    }

    private boolean isFirstTime() {
        return firstTime;
    }

    public void setFirstTime(boolean firstTime) {
        if (firstTime) {
            btnRefresh.setVisible(false);
        }
        this.firstTime = firstTime;
    }

    /**
     * @return the restartRequired
     */
    public boolean isRestartRequired() {
        return restartRequired;
    }

    /**
     * @param restartRequired the restartRequired to set
     */
    public void setRestartRequired(boolean restartRequired) {
        this.restartRequired = restartRequired;
    }

    private void makeActions() {
        outputsAction = new ManageOutputsAction(this);
    }

    private class MPDTester implements Runnable {

        public void run() {
            try {
                MPD mpd = JukeboxProperties.getInstance().getMPD();
                try {
                    mpd.close();
                } catch (Exception e) {
                    //don't care
                }
                JukeboxProperties.getInstance().resetMPD();
                mpd = JukeboxProperties.getInstance().getMPD();
                if (mpd != null) {
                    setConnectionSuccessful(true);
                }
            } catch (Exception e) {
                sp.setStopped(true);
                setConnectionSuccessful(false);
                JOptionPane.showMessageDialog(Utils.getParentComponent(panel), "A connection error has occurred.", "Error", JOptionPane.ERROR_MESSAGE);
            } finally {
                sp.setStopped(true);
            }

            if (isConnectionSuccessful()) {
                Utils.getParentComponent(panel).dispose();
                StartMPD.runApp();
            } else {
                setVisible(true);
            }
        }
    }

    public boolean isConnectionSuccessful() {
        return connectionSuccessful;
    }

    public void setConnectionSuccessful(boolean connectionSuccessful) {
        this.connectionSuccessful = connectionSuccessful;
    }

    private void setPasswordFields(boolean b) {
        jLabel8.setEnabled(b);
        tfPassword.setEnabled(b);
    }

    private String getPassword() {
        return (new String(tfPassword.getPassword()));
    }

    private void setPassword(String password) {
        if (password != null && password.length() > 0) {
            cbPassword.setSelected(true);
        }
        tfPassword.setText(password);
    }

    public void setExitAction(String exitAction) {
        cbExitAction.setSelectedItem(exitAction);
    }

    public String getExitAction() {
        return (String) cbExitAction.getSelectedItem();
    }

    public LAFPanel getLAFPanel() {
        return (LAFPanel) lafPanel;
    }

    public boolean getRememberState() {
        return chkboxLastState.isSelected();
    }

    public void setRememberState(boolean rememberState) {
        chkboxLastState.setSelected(rememberState);
    }
}
